/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.serviceimpl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.compass.annotations.config.CompassAnnotationsConfiguration;
import org.compass.core.Compass;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.converter.basic.DateConverter;

import com.sesame.domain.Album;
import com.sesame.domain.Image;
import com.sesame.domain.User;
import com.sesame.service.SearchService;

/**
 * @author voidman
 * 
 */
public class SearchServiceImpl implements SearchService {

    private String directory;
    private Compass compassUser;
    private Compass compassAlbum;
    private Compass compassImage;

    public void setDirectory(String directory) {
        this.directory = directory;

    }

    /**
     * Init compass.
     */
    public void init() {
        DateConverter dateConverter = new DateConverter();
        dateConverter.setFormat("yyyy-MM-dd");
        compassUser = new CompassAnnotationsConfiguration().setConnection(
                directory).addClass(User.class).registerConverter("date",
                dateConverter).buildCompass();
        compassAlbum = new CompassAnnotationsConfiguration().setConnection(
                directory).addClass(Album.class).registerConverter("date",
                dateConverter).buildCompass();
        compassImage = new CompassAnnotationsConfiguration().setConnection(
                directory).addClass(Image.class).registerConverter("date",
                dateConverter).buildCompass();
    }

    /**
     * Destroy compass.
     */
    public void destroy() {
        compassUser.close();
        compassAlbum.close();
        compassImage.close();
    }

    public void index(Object obj) {
        CompassSession session = null;
        CompassTransaction tx = null;
        try {
            if (obj instanceof User) {

                session = compassUser.openSession();
            } else if (obj instanceof Album) {

                session = compassAlbum.openSession();
            } else if (obj instanceof Image) {

                session = compassImage.openSession();
            } else {

            }
            tx = session.beginTransaction();
            session.create(obj);
            tx.commit();

        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            if (session != null)
                session.close();
        }
    }

    public void unindex(Object obj) {

        CompassSession session = null;
        CompassTransaction tx = null;
        try {
            if (obj instanceof User) {
                session = compassUser.openSession();
            } else if (obj instanceof Album) {
                session = compassAlbum.openSession();
            } else if (obj instanceof Image) {
                session = compassImage.openSession();
            } else {

            }
            tx = session.beginTransaction();
            session.delete(obj);
            tx.commit();

        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            if (session != null)
                session.close();
        }
    }

    public void reindex(Object obj) {

        CompassSession session = null;
        CompassTransaction tx = null;
        try {
            if (obj instanceof User) {
                session = compassUser.openSession();
            } else if (obj instanceof Album) {
                session = compassAlbum.openSession();
            } else if (obj instanceof Image) {
                session = compassImage.openSession();
            } else {

            }
            tx = session.beginTransaction();
            session.delete(obj);
            session.create(obj);
            tx.commit();

        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            if (session != null)
                session.close();
        }
    }

    @SuppressWarnings("unchecked")
    public List<Object> search(String q, String type) {

        if (q == null)
            return Collections.EMPTY_LIST;
        q = q.trim();
        if ("".equals(q))
            return Collections.EMPTY_LIST;
        CompassSession session = null;
        CompassTransaction tx = null;
        try {
            if (type.equals("user")) {
                session = compassUser.openSession();
            } else if (type.equals("album")) {
                session = compassAlbum.openSession();
            } else if (type.equals("image")) {
                session = compassImage.openSession();
            } else {

            }

            tx = session.beginTransaction();
            CompassHits hits = session.find(q);
            int count = hits.length();

            if (count == 0) {
                tx.commit();
                return Collections.EMPTY_LIST;
            }
            List<Object> results = new ArrayList<Object>();
            if (type.equals("user")) {

                for (int i = 0; i < count; i++) {
                    results.add((User) hits.data(i));

                }

            } else if (type.equals("album")) {

                for (int i = 0; i < count; i++) {
                    results.add((Album) hits.data(i));

                }

            } else if (type.equals("image")) {

                for (int i = 0; i < count; i++) {
                    results.add((Image) hits.data(i));

                }

            } else {

            }
            tx.commit();
            return results;

        } catch (RuntimeException e) {
            tx.rollback();
            throw e;
        } finally {
            if (session != null)
                session.close();
        }
    }

}